import { admin } from '../../admin';

class TMoApp {
    constructor() {
        this.sysCode = "znSys003";
        this.userInfo = {};
        this.sysRoles = [];
    }
    checkLocalUserInfo() {
        var me = this;
        var res = {
            status: 0
        };
        return new Promise((su, fa) => {
            try {
                var localKey = "znTH_moapp_login";
                var loginData = window.localStorage.getItem(localKey);
                if (loginData && loginData != null && loginData.length > 10) {
                    var loginInfo = JSON.parse(loginData);
                    me.userInfo = loginInfo.userInfo;
                    me.sysRoles = loginInfo.sysRoles;
                    res.status = 1;
                    su(res);
                }
            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }
    checkLogin(uid, pwd) {
        var me = this;
        var res = {
            status: 0,
            userInfo: {},
            sysRoles: []
        };
        return new Promise((su, fa) => {
            try {
                admin.api.execProc("proc_check_login", {
                    userCode: uid,
                    pwd: pwd
                }).then(_res => {
                    console.log(_res);
                    var success = _res && _res != null && _res.queryStatus && _res.queryStatus == 1 && _res.datasetList[0].length > 0;
                    if (success) {
                        res.userInfo = _res.datasetList[0];
                        res.sysRoles = _res.datasetList[1];
                        res.status = 1;
                        var localKey = "znTH_moapp_login";
                        window.localStorage.setItem(localKey, JSON.stringify(res));
                    } else {
                        window.localStorage.removeItem(localKey)
                        res.status = -1;
                        res.error = "用户名或密码错误！"
                    }
                    su(res)
                })
            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }
    getIndexDict(sysCode) {
        var me = this;
        var res = {
            status: 0,
            cates: [],
            devices: []
        };
        return new Promise((su, fa) => {
            try {
                var dataKey = "admin/moapp/index/dict.txt";
                var ps = { sysCode };
                admin.api.query(dataKey, ps).then(_res => {
                    console.log(_res);
                    res = {
                        cates: _res.datasetList[0],
                        devices: _res.datasetList[1],
                        variants: _res.datasetList[2],
                        status: 1
                    }
                    res.cates.forEach(c => {
                        c.devices = res.devices.filter(d => {
                            d.vars = {};
                            d.values = {};
                            return d.category == c.category
                        })
                    })
                    res.devices.forEach(d => {
                        var vars = res.variants.filter(v => {
                            return v.deviceId == d.deviceId;
                        })
                        vars.forEach(v => {
                            v.value = {};
                            d.vars[v.varCode] = v
                            d.values[v.varCode] = {}
                        })
                    })
                    su(res)
                })
            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }
    getVarRealValues(varIdList) {
        var me = this;
        var res = {
            status: 0,
            cates: [],
            devices: []
        };
        return new Promise((su, fa) => {
            try {
                var ps = {
                    varIdList: varIdList
                }
                admin.api.callApi("znVariant", 'getReal', ps).then(_res => {
                    su(_res)
                })
            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }
    getDeviceInfo(deviceId) {
        var me = this;
        var res = {};
        return new Promise((su, fa) => {
            try {
                var dataKey = "admin/moapp/detail/deviceInfo.txt";
                var ps = { deviceId };
                admin.api.query(dataKey, ps).then(_res => {
                    console.log(_res);
                    res = (_res.queryStatus == 1 && _res.recordset && _res.recordset.length > 0) ? _res.recordset[0] : 0;
                    su(res)
                })
            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }
    getVarDayValues(varId, dateStr) {
        var me = this;
        var res = [];
        return new Promise((su, fa) => {
            try {
                var ps = {
                    varId,
                    dateStr
                }
                admin.api.callApi("znVariant", 'getVarDayValues', ps).then(_res => {
                    console.log(_res)
                    if (_res.status == 1) {
                        var datas = _res.result.split("\n");
                        res = datas.map(item => {
                            var vs = item.split(",");
                            if (vs[2] && vs[2] > 0) {
                                var r = {
                                    v: parseFloat(parseFloat(vs[1]).toFixed(1)),
                                    t: new Date(1000 * vs[2]),
                                }
                                r.ts = r.t.Format("yyyy-MM-dd HH:mm:00");
                                return r
                            } else {
                                return null
                            }
                        }).filter(item => {
                            return item != null;
                        })
                    }
                    su(res)
                })
            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }
    getRealInfo(varId, thresholdValue = 60) {
        var me = this;
        var res = {
            status: 0,
            t: 0,
            dt: "",
            v: 0,
            value: "",
            warn: {
                ws: 0,
                label: ""
            }
        };
        return new Promise((su, fa) => {
            try {
                me.getVarRealValues([varId]).then(_res => {
                    console.log(_res)
                    if (_res.status == 1 && _res.result[varId]) {
                        res.t = _res.result[varId].t;
                        res.v = _res.result[varId].v;
                        res.dt = (new Date(res.t * 1000)).Format("yyyy-MM-dd HH:mm");
                        res.value = parseFloat(res.v).toFixed(1);
                        res.status = 1;
                        res.warn.ws = res.v > thresholdValue ? 1 : 0;
                        res.warn.label = res.v > thresholdValue ? "超高" : "正常";
                    }
                    su(res)
                })

            } catch (error) {
                console.log(error);
                su(res)
            }
        })
    }


}
const moApp = new TMoApp()
export { moApp }